//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public final class Idcard {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    public static final int[] w;
    public static final Map<Integer, String> provinces;

    static {
        sdf.setLenient(false);
        w = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
        provinces = new HashMap();
        provinces.put(11, "北京市");
        provinces.put(12, "天津市");
        provinces.put(13, "河北省");
        provinces.put(14, "山西省");
        provinces.put(15, "内蒙古自治区");
        provinces.put(21, "辽宁省");
        provinces.put(22, "吉林省");
        provinces.put(23, "黑龙江省");
        provinces.put(31, "上海市");
        provinces.put(32, "江苏省");
        provinces.put(33, "浙江省");
        provinces.put(34, "安徽省");
        provinces.put(35, "福建省");
        provinces.put(36, "江西省");
        provinces.put(37, "山东省");
        provinces.put(41, "河南省");
        provinces.put(42, "湖北省");
        provinces.put(43, "湖南省");
        provinces.put(44, "广东省");
        provinces.put(45, "广西壮族自治区");
        provinces.put(46, "海南省");
        provinces.put(50, "重庆市");
        provinces.put(51, "四川省");
        provinces.put(52, "贵州省");
        provinces.put(53, "云南省");
        provinces.put(54, "西藏自治区");
        provinces.put(61, "陕西省");
        provinces.put(62, "甘肃省");
        provinces.put(63, "青海省");
        provinces.put(64, "宁夏回族自治区");
        provinces.put(65, "新疆维吾尔自治区");
    }

    public Idcard() {
    }

    public static String to18(String idcard) throws ParseException {
        switch (idcard.length()) {
            case 15:
                String id17 = idcard.substring(0, 6) + "19" + idcard.substring(6);
                idcard = id17 + makeISOMod(id17);
                break;
            case 16:
            case 17:
            default:
                throw new RuntimeException("invalid length : " + idcard);
            case 18:
                verifyISOMod(idcard);
        }

        verifyAddress(idcard);
        verifyBirthday(idcard);
        return idcard;
    }

    public static boolean isFemale(String idcard) {
        return (getSerial(idcard) & 1) == 0;
    }

    public static int getSerial(String idcard) {
        return Integer.valueOf(idcard.substring(14, 17));
    }

    public static int getYear(String idcard) {
        return Integer.valueOf(idcard.substring(6, 10));
    }

    public static int getMonth(String idcard) {
        return Integer.valueOf(idcard.substring(10, 12));
    }

    public static int getDay(String idcard) {
        return Integer.valueOf(idcard.substring(12, 14));
    }

    public static String getBirthday(String idcard) {
        return idcard.substring(6, 14);
    }

    public static int getProvince(String idcard) {
        return Integer.valueOf(idcard.substring(0, 2));
    }

    public static int getAge(String idcard) {
        return getAge(getYear(idcard), getMonth(idcard), getDay(idcard));
    }

    public static int getAge(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        int age = cal.get(1) - year;
        int dmonth = 1 + cal.get(2) - month;
        if (dmonth < 0) {
            return age - 1;
        } else if (dmonth > 0) {
            return age;
        } else {
            int dday = cal.get(5) - day;
            return dday < 0 ? age - 1 : age;
        }
    }

    public static boolean isLeapYear(int year) {
        return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
    }

    public static int dayOfMonth(int year, int month) {
        switch (month) {
            case 2:
                return isLeapYear(year) ? 29 : 28;
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            default:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
        }
    }

    public static int sumISOMod(String idcard) {
        int sum = 0;
        int end = idcard.length() - 1;
        char last = idcard.charAt(end);
        if (last != 'x' && last != 'X') {
            ++end;
        } else {
            sum += 10;
        }

        for(int i = 0; i < end; ++i) {
            sum += w[i] * (idcard.charAt(i) - 48);
        }

        return sum;
    }

    public static char makeISOMod(String idcard) {
        int idx = sumISOMod(idcard) % 11;
        switch (idx) {
            case 0:
                return '1';
            case 1:
                return '0';
            case 2:
                return 'x';
            default:
                return Character.forDigit(12 - idx, 10);
        }
    }

    public static void verifyISOMod(String idcard) {
        int sum = sumISOMod(idcard);
        if (sum % 11 != 1) {
            throw new RuntimeException("invalid isomod11-2 : " + idcard);
        }
    }

    public static void verifyBirthday(String idcard) throws ParseException {
        sdf.parse(getBirthday(idcard));
        int age = getAge(idcard);
        if (age < 0 || age > 150) {
            throw new RuntimeException("age out of range [0, 100] : " + idcard);
        }
    }

    public static void verifyAddress(String idcard) {
        int province = getProvince(idcard);
        if (provinces.get(province) == null) {
            throw new RuntimeException("invalid province : " + idcard);
        }
    }

    public static void test(int i, String idcard, String want) {
        try {
            String result = to18(idcard);
            if (want != null && result.equals(want)) {
                return;
            }

            System.out.println("test failed! i=" + i + " idcard=" + idcard + " want=" + want + " result=" + result);
        } catch (Exception var4) {
            if (want == null) {
                return;
            }

            System.out.println("test failed! i=" + i + " idcard=" + idcard + " want=" + want + " exp=" + var4);
        }

    }

    public static void main(String[] args) throws Exception {
        for(int i = 0; i < args.length; ++i) {
            Random r = new Random(System.currentTimeMillis());
            String sp = null;
            int idx = r.nextInt() % provinces.size();
            Iterator var6 = provinces.keySet().iterator();

            while(var6.hasNext()) {
                Integer p = (Integer)var6.next();
                sp = p.toString();
                --idx;
                if (idx < 0) {
                    break;
                }
            }

            String idcard18 = to18(sp + "0101" + args[i] + "010");
            System.out.println(idcard18 + " age=" + getAge(idcard18));
        }

        String[] in = new String[]{"352202760110425", "35220219760110425x", "440524800101001", "440524198001010013", "440524188001010014", "440524188001010014", "11010519491231002x", "11010519491231002x", "11010519491231002X", "11010519491231002X", "35220219760110425X", "35220219760110425X", "35220219760110425x", "35220219760110425x", "352202760229425", "352202197602294251", "352202761310425", null, "352202761100425", null, "352202770229425", null, "4405248001011", null, "993202760110425", null};

        for(int i = 0; i < in.length; i += 2) {
            test(i, in[i], in[i + 1]);
        }

    }
}
